package com.esms.controller;

import com.esms.dao.EmpDepartmentCustomVoMapper;
import com.esms.dao.EmployeeMapper;
import com.esms.po.DepGrand;
import com.esms.po.Employee;
import com.esms.po.Generality;
import com.esms.po.QassessMent;
import com.esms.service.impl.EmployeeDepGrandServerImpl;
import com.esms.service.impl.EmployeeTagDetailServerImpl;
import com.esms.vo.EmployeeDepGrandVO;
import com.esms.vo.EmployeeTagDetailVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 个性化指标Controller
 * @Author: ko
 * @DATE: 2022/2/19 09:23
 */

@Controller
@RequestMapping("/empDepartmentScore")
public class EmployeeDepScoreController {
	
	@Autowired
	private EmployeeDepGrandServerImpl EmployeeDepGrandService;
	
	@Autowired
	private EmpDepartmentCustomVoMapper empDepartmentCustomVoMapper  = null;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;

	@Autowired
	private EmployeeTagDetailServerImpl employeeTagDetailServer;


	
	/**
	 * 查找被评价部门人员信息
	 * @param bpjbmDId   被评价部门ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findDempSelective.do")
    @ResponseBody
	public EmployeeDepGrandVO findDempSelective(
			@RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam String bpjbmDId,
            @RequestParam String qTargetid,
            HttpSession httpSession) throws Exception {

		/**
		 * select e.e_account,e.e_name,d.d_name,d.d_id,q.q_targetscore from
		        employee e
		        left join department d on e.d_id = d.d_id
		        left join q_assessment q on e.e_account = q.q_daccount and q.q_targetid = #{arg1}
			where  e.d_id = #{arg0} and e.e_isdel='1'
			order by e.e_account;
		 * **/
		List<DepGrand> grandList = new ArrayList<DepGrand>();
		try{
			int eId = (Integer) httpSession.getAttribute("employeeId");
			grandList = EmployeeDepGrandService.getEmpDeployeeGrandList(eId,bpjbmDId,qTargetid);
		}catch (Exception e){
			e.printStackTrace();
		}

        //设置前台需要的数据
        EmployeeDepGrandVO employeeGrandVO = new EmployeeDepGrandVO();
        employeeGrandVO.setCode(0);
        employeeGrandVO.setMsg("");
        employeeGrandVO.setCount(grandList.size());
        employeeGrandVO.setData(grandList);
        return employeeGrandVO;
    }


	/**
	 * 查找指标详情
	 * @param qTargetid   被评价指标集合
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findTagDetailInfo.do")
	@ResponseBody
	public EmployeeTagDetailVO findTagDetailInfo(
			@RequestParam(value="page", defaultValue="1")int pageNum,
			@RequestParam(value="limit", defaultValue="5") int limit,
			@RequestParam String qTargetid,
			HttpSession httpSession) throws Exception {

		List<Generality> employeeTagDetailList = new ArrayList<Generality>();
		String[] arrs = qTargetid.split(",");
		int[] qTargetids = StringToInt(arrs);
		try{
			employeeTagDetailList = employeeTagDetailServer.getEmployeeTagDetailList(qTargetids);
		}catch (Exception e){
			e.printStackTrace();
		}

		//设置前台需要的数据
		EmployeeTagDetailVO employeeTagDetailVO = new EmployeeTagDetailVO();
		employeeTagDetailVO.setCode(0);
		employeeTagDetailVO.setMsg("");
		employeeTagDetailVO.setCount(employeeTagDetailList.size());
		employeeTagDetailVO.setData(employeeTagDetailList);
		return employeeTagDetailVO;
	}


    @RequestMapping("/updateDepGrand.do")
    @ResponseBody
    public EmployeeDepGrandVO updateDepGrandAAA(
    		@RequestParam("list") String list,
    		@RequestParam String dId,
    		@RequestParam String qTargetid,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg="";
    	ObjectMapper mapper = new ObjectMapper();
    	Date currentTime = new Date(); 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, DepGrand.class);
    	List<DepGrand> listDepGrand =  (List<DepGrand>)mapper.readValue(list, jt);
    	//当前登录员工信息
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	
    	int currentDid = employee.getdId();//当前评价人员的部门ID
    	String eAccount = employee.geteAccount();//当前评价人员的工号
    	
    	QassessMent qassessMent = new QassessMent();
    	for(int i=0;i<listDepGrand.size();i++){
    		DepGrand depGrandData = listDepGrand.get(i);
    		
    		String qDaccount = depGrandData.geteAccount();//被评价者的工号
    		int pjDid = Integer.valueOf(depGrandData.getdId());//被评价者的部门ID
    		int qTargetscore1 = depGrandData.getqTargetscore1();//分数1
		    int qTargetscore2 = depGrandData.getqTargetscore2();//分数1
		    int qTargetscore3 = depGrandData.getqTargetscore3();//分数1
		    int qTargetscore4 = depGrandData.getqTargetscore4();//分数1
		    int qTargetscore5 = depGrandData.getqTargetscore5();//分数1

		    int qTargetscore6 = depGrandData.getqTargetscore6();//分数1
		    int qTargetscore7 = depGrandData.getqTargetscore7();//分数1
		    int qTargetscore8 = depGrandData.getqTargetscore8();//分数1
		    int qTargetscore9 = depGrandData.getqTargetscore9();//分数1
		    int qTargetscore10 = depGrandData.getqTargetscore10();//分数1

		    int qTargetscore11 = depGrandData.getqTargetscore11();//分数1
		    int qTargetscore12 = depGrandData.getqTargetscore12();//分数1
		    int qTargetscore13 = depGrandData.getqTargetscore13();//分数1
		    int qTargetscore14 = depGrandData.getqTargetscore14();//分数1
		    int qTargetscore15 = depGrandData.getqTargetscore15();//分数1

		    int qTargetscore16 = depGrandData.getqTargetscore16();//分数1
		    int qTargetscore17 = depGrandData.getqTargetscore17();//分数1
		    int qTargetscore18 = depGrandData.getqTargetscore18();//分数1
		    int qTargetscore19 = depGrandData.getqTargetscore19();//分数1
		    int qTargetscore20 = depGrandData.getqTargetscore20();//分数1

		    int qTargetscore21 = depGrandData.getqTargetscore21();//分数1
		    int qTargetscore22 = depGrandData.getqTargetscore22();//分数1
		    int qTargetscore23 = depGrandData.getqTargetscore23();//分数1
		    int qTargetscore24 = depGrandData.getqTargetscore24();//分数1
		    int qTargetscore25 = depGrandData.getqTargetscore25();//分数1

    		String qCreateQuarter = GetQuarterByDate(formatter.format(currentTime));//计算当前日期属于第几季度
    		
    		qassessMent.setqAaccount(eAccount);//评价人员工号
    		qassessMent.setqDaccount(qDaccount);//被评人员工号
    		qassessMent.setqAdid(currentDid);//评价人员所属部门
    		qassessMent.setqDdid(pjDid);//被评人员所属部门
    		qassessMent.setqCreateTime(currentTime);//创建时间
    		qassessMent.setqCreateQuarter(qCreateQuarter);//归属季度
    		qassessMent.setqTargetid(qTargetid);//评价指标
		    qassessMent.setqTag(qTargetid);
    		qassessMent.setqTargetscore1(qTargetscore1);//评价分数

		    qassessMent.setqTargetscore2(qTargetscore2);//评价分数
		    qassessMent.setqTargetscore3(qTargetscore3);//评价分数
		    qassessMent.setqTargetscore4(qTargetscore4);//评价分数
		    qassessMent.setqTargetscore5(qTargetscore5);//评价分数

		    qassessMent.setqTargetscore6(qTargetscore6);//评价分数
		    qassessMent.setqTargetscore7(qTargetscore7);//评价分数
		    qassessMent.setqTargetscore8(qTargetscore8);//评价分数
		    qassessMent.setqTargetscore9(qTargetscore9);//评价分数
		    qassessMent.setqTargetscore10(qTargetscore10);//评价分数

		    qassessMent.setqTargetscore11(qTargetscore11);//评价分数
		    qassessMent.setqTargetscore12(qTargetscore12);//评价分数
		    qassessMent.setqTargetscore13(qTargetscore13);//评价分数
		    qassessMent.setqTargetscore14(qTargetscore14);//评价分数
		    qassessMent.setqTargetscore15(qTargetscore15);//评价分数

		    qassessMent.setqTargetscore16(qTargetscore16);//评价分数
		    qassessMent.setqTargetscore17(qTargetscore17);//评价分数
		    qassessMent.setqTargetscore18(qTargetscore18);//评价分数
		    qassessMent.setqTargetscore19(qTargetscore19);//评价分数
		    qassessMent.setqTargetscore20(qTargetscore20);//评价分数

		    qassessMent.setqTargetscore21(qTargetscore21);//评价分数
		    qassessMent.setqTargetscore22(qTargetscore22);//评价分数
		    qassessMent.setqTargetscore23(qTargetscore23);//评价分数
		    qassessMent.setqTargetscore24(qTargetscore24);//评价分数
		    qassessMent.setqTargetscore25(qTargetscore25);//评价分数

    		qassessMent.setqTargetscoresum(0);//指标分数汇总,先就放当前分数
    		qassessMent.setqTargetscorecalu(0);//指标分数汇总,先就放当前分数
    		qassessMent.setqByzd("");
    		qassessMent.setqByzd1("");
    		try {
    			/**根据：评分人员部门&&被评分人员工号&&标签进行删除**/
        		EmployeeDepGrandService.deleteDepGrand(eAccount,qDaccount,qTargetid);
        		/**重新插入新的分数数据**/
        		req = EmployeeDepGrandService.insertDepGrand(qassessMent);
        		if(req < 0) {
        			reMsg = "勤绩考核评分失败";
          		}else {
          			reMsg = "勤绩考核评分成功";
          		}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
        //设置前台需要的数据
        EmployeeDepGrandVO employeeGrandVO = new EmployeeDepGrandVO();
        employeeGrandVO.setCode(0);
        employeeGrandVO.setMsg(reMsg);
        employeeGrandVO.setData(listDepGrand);
        return employeeGrandVO;
    }
    
    
    
    /**
     * 计算当前日期是一年中第几季度：返回值：年+季度：2022+01/02/03/04
     * **/
	public static String GetQuarterByDate(String date) throws Exception{
		
		if(date == ""|| "".equals(date)){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date datePar = sdf.parse(date);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datePar);
		
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		int mouth = datePar.getMonth()+1;
		
		if(mouth>=1 && mouth<=3){
			return year + "01";//第一季度
		}else if(mouth>=4 && mouth<=6){
			return year + "02";//第二季度
		}else if(mouth>=7 && mouth<=9){
			return year + "03";//第三季度
		}else if(mouth>=10 && mouth<=12){
			return year + "04";//第四季度
		}else{
			return "000000";
		}
	}
	/**
	 * String数组转int数组
	 * **/
	public static int[] StringToInt(String[] arrs){
		int[] ints = new int[arrs.length];
		for(int i=0;i<arrs.length;i++){
			ints[i] = Integer.parseInt(arrs[i]);
		}
		return ints;
	}

	/**测试代码**/
	public static void main(String args[]) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		try {
			String fff = GetQuarterByDate(dateString);
			System.out.println("aaa==="+fff);

			String aaa = "1,2,3,4,5";
			String[] bbb = aaa.split(",");
			System.out.println(StringToInt(bbb));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
